#!/usr/bin/env python
# -*- coding: utf-8 -*-
# $Id: TestDjango.py 3096 2010-07-06 07:21:14Z Bear $
#
# Copyright (c) 2010 Nuwa Information Co., Ltd, and individual contributors.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#   1. Redistributions of source code must retain the above copyright notice,
#      this list of conditions and the following disclaimer.
#
#   2. Redistributions in binary form must reproduce the above copyright
#      notice, this list of conditions and the following disclaimer in the
#      documentation and/or other materials provided with the distribution.
#
#   3. Neither the name of Nuwa Information nor the names of its contributors
#      may be used to endorse or promote products derived from this software
#      without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# $Author: Bear $
# $Date: 2010-07-06 15:21:14 +0800 (星期二, 06 七月 2010) $
# $Revision: 3096 $

import os, sys

path = os.path.abspath(os.curdir)

try:
    from Iuppiter import DistUtil
    sys.path.insert(0, DistUtil.getExtensionPath(os.path.dirname(path), 
                                                 mode="Release"))
except ImportError:
    pass

from django.core.handlers.wsgi import WSGIHandler
app = WSGIHandler()

sys.path.insert(0, os.path.join(path, "app"))

os.environ["DJANGO_SETTINGS_MODULE"] = "settings"

import threading
import time
import atexit

import PyCaduceus

#threads = 20
#scheduler = PyCaduceus.CreatePionSingleServiceSchedulerFactory(threads)
#scheduler = PyCaduceus.CreatePionOneToOneSchedulerFactory(threads)
#server = PyCaduceus.CreateWebServer(app, "/", 8009, scheduler)
server = PyCaduceus.CreateWebServer(app, "/", 8009)

server.setServiceOption('/', 'info', '1')

def longrun():
    server.start()
    server.join()

atexit.register(server.stop)
#t = threading.Thread(target=server.start)
#t = threading.Thread(target=longrun)
#t.setDaemon(True)
#t.start()
server.start()
#server.join()
i = 0
while True:
    #if i > 20:
    #    print 'exit...'
    #    break
    print 'Still alive...'
    i += 1
    time.sleep(5)
